package com.itwn.comic.service;


import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.itwn.comic.entity.Remark;
import com.itwn.comic.mapper.RemarkMapper;
import com.itwn.comic.mapper.UserMapper;
import com.itwn.comic.vo.RemarkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cx
 * @date 2022/2/18
 * @apiNote
 * 处理评论的crud包括图片的处理
 * 评论CRUD(时间排序查询时)remark | 举报的crud有两种
 * t_remark
 */
@Service
public class RemarkService{

    @Autowired
    RemarkMapper remarkMapper;

    @Autowired
    UserMapper userMapper;
    /**
     * 删除评论,子级评论删除后减少父级评论的评论数
     * @param id
     */
    @Transactional
    public void removeByKey(long id) {
            this.remarkMapper.deleteByPrimaryKey(id);
    }

    /**
     * 添加评论,子级评论添加后增加父级评论的评论数
     */
    @Transactional
    public void add(Remark remark){
        this.remarkMapper.insert(remark);
        if(remark.getParentId()!=null && remark.getParentId() >0){
            this.remarkMapper.updateDecRemCount(remark.getParentId());
        }
    }

    /**
     * 修改评论
     */
    public void modify(Remark remark){
        this.remarkMapper.update(remark);
    }

    /**
     * 查询评论
     * @param now
     * @param size
     * @param
     * @return
     */
    public PageInfo<RemarkVo> queryByPage(Integer now, Integer size, Integer pid,
                                          String comicName, String username){
        PageMethod.startPage(now,size);

        List<RemarkVo> list = this.remarkMapper.selectAll(pid,comicName,username);
        return new PageInfo<>(list);
    }


    public Remark queryRemarkById(Integer id) {
        return this.remarkMapper.selectByPrimaryKey(id);
    }

    public void modifyStatus(int id,int status) {
        this.remarkMapper.updateStatus(id,status);
    }
}
